﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace PathFinder
{
    public partial class MainForm : Form
    {
        #region class definitions
        private NewProjectForm proj;
        private Size previous;
        private GraphPoint selectedPoint = new GraphPoint(-1,-1);
        private GraphLine selectedLine;
        private bool isselectedMoveitem = false;
        private int massSelectIndex = 0;
        public static MainForm instance;
        public DrawManager drawManager;
        private Thread thr;
        PropertiesManager propertiesManager;
        Project project;
        public MainForm()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Icon;
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.DrawingWindow});
            DrawingWindow.ResumeLayout(false);
            instance = this;
            DrawLineButton.Enabled = false;
            MoveButton.Enabled = false; 
            previous = this.Size;
            DeleteButton.Enabled = false;
            PropertiesButton.Enabled = false;
            StartButton.Enabled = false;
            EndButton.Enabled = false;
            SelectedLineGroupBox.Visible = false;
            MainProgramMenu.MenuActivate += new EventHandler(MainProgramMenu_MenuActivate);
            propertiesManager = new PropertiesManager(TextBoxNameLine,SlopePreviewPictureBox,SlopeComboBox,DirectionPowerComboBox,
                QualityComboBox,IsRequiredCheckBox,LengthTextBox,SavePropertiesButton,CancelPropertiesButton);

        }
        #endregion

        #region Drawing functions
        //DONE
        private void DrawingMouseMove(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            GraphLine PointlessGraphLine = null;
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.DrawCreatingLine(OnGraphPoint.point);
                return;
            }

            Point OnGraphLine = drawManager.GetClosestPointToLine(pt, ref PointlessGraphLine);
            if (OnGraphLine.X != -1)
            {
                drawManager.DrawCreatingLine(OnGraphLine);
            }
            else
            {
                drawManager.DrawCreatingLine(pt);
            }
        }
        //DONE
        private void DrawingMouseClick(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            GraphLine OnGraphLine = null;
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.ClickOnExistingPoint_DrawingMode(OnGraphPoint);
                return;
            }
            Point onLine = drawManager.GetClosestPointToLine(pt,ref OnGraphLine);
            if (onLine.X != -1)
            {
                drawManager.SelectPoint(pt);
                drawManager.ClickOnExistingLine_DrawingMode(onLine,OnGraphLine);
            }
            else
            {
                drawManager.ClickOnBlank_DrawingMode(pt);
            }
        }

        #endregion

        #region Deleting functions
        //DONE
        private void DeletingMouseClick(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            GraphLine OnGraphLine = null;
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.DeletePoint(OnGraphPoint);
                return;
            }
            drawManager.GetClosestPointToLine(pt, ref OnGraphLine);
            if (OnGraphLine != null)
                drawManager.DeleteClick(OnGraphLine);
        }
        //DONE
        private void DeletingMouseMove(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            GraphLine OnGraphLine = null;
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.RedrawDeletingPoint(OnGraphPoint);
                return;
            }
            else
            {
                drawManager.GetClosestPointToLine(pt, ref OnGraphLine);
                if (OnGraphLine != null)
                {
                    drawManager.RedrawDeletingLine(OnGraphLine);
                    return;
                }
            }
            drawManager.Redraw();
        }

        #endregion

        #region Moving functions

        private void MovingMouseMove(Point pt)
        {
            if (selectedPoint != null)
            {
                GraphPoint point = drawManager.FindOverlappingPoint(selectedPoint);
                selectedPoint.point = ValidPoint(pt);
                if (selectedPoint.IsNear(point.point) && point.point.X != -1)
                {
                    selectedPoint.point = point.point;
                }
                else
                {
                    GraphLine OnGraphLine = null;
                    Point onLine = drawManager.FindOverlappingLine(pt, ref OnGraphLine, selectedPoint);
                    if (onLine.X != -1)
                    {
                        selectedPoint.point = onLine;
                    }
                }
                drawManager.Redraw();
            }
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.RedrawMovingPoint(OnGraphPoint);
                isselectedMoveitem = true;
            }
            else if (isselectedMoveitem)
            {
                drawManager.Redraw();
                isselectedMoveitem = false;
            }
        }

        #endregion

        #region Properties functions

        private void PropertiesMouseMove(Point pt)
        {
            GraphLine OnGraphLine = null;
            Point onLine = drawManager.GetClosestPointToLine(pt, ref OnGraphLine);
            if (OnGraphLine != null && SelectedLineGroupBox.Visible == false)
            {
                drawManager.RedrawPropertiesLine(OnGraphLine);
                return;
            }
            drawManager.RedrawWithSelectedLine(selectedLine);
        }

        private void PropertiesMouseClick(Point pt)
        {

            GraphLine OnGraphLine = null;
            drawManager.GetClosestPointToLine(pt, ref OnGraphLine);
            if (OnGraphLine != null && SelectedLineGroupBox.Visible == false)
            {
                selectedLine = OnGraphLine;
                drawManager.selectedLine = selectedLine;
                SelectedLineGroupBox.Visible = true;
                propertiesManager.Load(OnGraphLine);
            }
        }

        #endregion

        #region StartButton functions

        private void StartButtonMouseMove(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            if (selectedPoint != OnGraphPoint && selectedPoint != null)
            {
                drawManager.Redraw();
            }
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.RedrawStartMouseOnPoint(OnGraphPoint);
                selectedPoint = OnGraphPoint;
            }
        }

        private void StartButtonMouseSelect(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.SelectStartPoint(OnGraphPoint);
                selectedPoint = OnGraphPoint;
                drawManager.Redraw();
            }
        }
        #endregion

        #region EndButton functions

        private void EndButtonMouseMove(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            if (selectedPoint != OnGraphPoint && selectedPoint != null)
            {
                drawManager.Redraw();
            }
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.RedrawEndMouseOnPoint(OnGraphPoint);
                selectedPoint = OnGraphPoint;
            }
        }

        private void EndButtonMouseSelect(Point pt)
        {
            GraphPoint OnGraphPoint = drawManager.SelectPoint(pt);
            if (OnGraphPoint.point.X != -1)
            {
                drawManager.SelectEndPoint(OnGraphPoint);
                selectedPoint = OnGraphPoint;
                drawManager.Redraw();
            }
        }
        #endregion

        #region Drawing window events

        private void DrawingWindow_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                DrawingWindow.VerticalScroll.Value = e.NewValue;
                DrawingWindow.VerticalScroll.Value = e.NewValue;
            }
            else
            {
                DrawingWindow.HorizontalScroll.Value = e.NewValue;
                DrawingWindow.HorizontalScroll.Value = e.NewValue;
            
            }
        }

        private void DrawingWindow_Click(object sender, EventArgs e)
        {
            MouseEventArgs evt = (MouseEventArgs)e;
            if (DrawLineButton.Checked)
            {
                DrawingMouseClick(evt.Location);
            }
            else if (DeleteButton.Checked)
            {
                DeletingMouseClick(evt.Location);
            }
            else if (PropertiesButton.Checked)
            {
                PropertiesMouseClick(evt.Location);
            }
            else if (StartButton.Checked)
            {
                StartButtonMouseSelect(evt.Location);
            }
            else if (EndButton.Checked)
            {
                EndButtonMouseSelect(evt.Location);
            }
        }

        private void DrawingWindow_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Location.X == -1) return;
            if (DrawLineButton.Checked)
            {
                DrawingMouseMove(e.Location);
            }
            if (DeleteButton.Checked)
            {
                DeletingMouseMove(e.Location);
            }
            else if (MoveButton.Checked)
            {
                MovingMouseMove(e.Location);
            }
            else if (PropertiesButton.Checked)
            {
                PropertiesMouseMove(e.Location);
            }
            else if (StartButton.Checked)
            {
                StartButtonMouseMove(e.Location);
            }
            else if (EndButton.Checked)
            {
                EndButtonMouseMove(e.Location);
            }
        }

        private void DrawingWindow_MouseDown(object sender, MouseEventArgs e)
        {
            if (MoveButton.Checked)
            {
                if (selectedPoint == null)
                {
                    selectedPoint = drawManager.SelectPoint(e.Location);
                }else if (selectedPoint.point.X == -1)
                {
                    selectedPoint = drawManager.SelectPoint(e.Location);
                }
            }
        }

        private void DrawingWindow_MouseUp(object sender, MouseEventArgs e)
        {
            if (MoveButton.Checked && selectedPoint != null)
            {
                
                GraphPoint point = drawManager.FindOverlappingPoint(selectedPoint);
                selectedPoint.point = ValidPoint(e.Location);
                if (selectedPoint.IsNear(point.point) && point.point.X != -1)
                {
                    if (selectedPoint.wayCount == 0)
                    {
                        selectedPoint = null;
                        return;
                    }
                    drawManager.MergePoints(selectedPoint, point);
                }
                else
                {
                    GraphLine OnGraphLine = null;
                    Point onLine = drawManager.FindOverlappingLine(e.Location, ref OnGraphLine, selectedPoint);
                    if (onLine.X != -1)
                    {
                        selectedPoint.point = onLine;
                        drawManager.ClickOnExistingLine_MovingMode(selectedPoint, OnGraphLine);
                    }
                }
                drawManager.Redraw();
            }
            selectedPoint = null;
        }

        private void drawablePicture_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (selectedLine != null)
                {
                    selectedLine = null;
                    drawManager.selectedLine = null;
                    SelectedLineGroupBox.Visible = false;
                }
                else
                {
                    DrawLineButton.Checked = false;
                    MoveButton.Checked = false;
                    DeleteButton.Checked = false;
                    PropertiesButton.Checked = false;
                    StartButton.Checked = false;
                    EndButton.Checked = false;
                    ChangeMode();
                }
            }
        }
       
        #endregion

        #region Buttons/Checkboxes events

        private void MoveButton_CheckedChanged(object sender, EventArgs e)
        {
            if (MoveButton.Checked && drawManager != null)
            {
                DrawLineButton.Checked = false;
                DeleteButton.Checked = false;
                PropertiesButton.Checked = false;
                SelectedLineGroupBox.Visible = false;
                StartButton.Checked = false;
                EndButton.Checked = false;
                ChangeMode();
            }
            DrawingWindow.Select();
        }

        private void PropertiesButton_CheckedChanged(object sender, EventArgs e)
        {
            if (!PropertiesButton.Checked && drawManager != null)
            {
                SelectedLineGroupBox.Visible = false;
            }
            else
            {
                DrawLineButton.Checked = false;
                MoveButton.Checked = false;
                DeleteButton.Checked = false;
                StartButton.Checked = false;
                EndButton.Checked = false;
                ChangeMode();
            }
            DrawingWindow.Select();
        }

        private void DeleteButton_CheckedChanged(object sender, EventArgs e)
        {
            if (DeleteButton.Checked && drawManager != null)
            {
                DrawLineButton.Checked = false;
                MoveButton.Checked = false;
                PropertiesButton.Checked = false;
                SelectedLineGroupBox.Visible = false;
                StartButton.Checked = false;
                EndButton.Checked = false;
                ChangeMode();
            }
            DrawingWindow.Select();
        }

        private void DrawLineButton_CheckedChanged(object sender, EventArgs e)
        {
            if (!DrawLineButton.Checked && drawManager != null)
            {
                drawManager.CutCreatingLine();
            }
            else
            {
                MoveButton.Checked = false;
                DeleteButton.Checked = false;
                PropertiesButton.Checked = false;
                SelectedLineGroupBox.Visible = false;
                StartButton.Checked = false;
                EndButton.Checked = false;
                ChangeMode();
            }
            DrawingWindow.Select();
        }

        private void StartButton_CheckedChanged(object sender, EventArgs e)
        {
            if (StartButton.Checked && drawManager != null)
            {
                DrawLineButton.Checked = false;
                MoveButton.Checked = false;
                PropertiesButton.Checked = false;
                DeleteButton.Checked = false;
                EndButton.Checked = false;
                SelectedLineGroupBox.Visible = false;
                ChangeMode();
            }
            DrawingWindow.Select();
        }

        private void EndButton_CheckedChanged(object sender, EventArgs e)
        {
            if (EndButton.Checked && drawManager != null)
            {
                DrawLineButton.Checked = false;
                MoveButton.Checked = false;
                PropertiesButton.Checked = false;
                SelectedLineGroupBox.Visible = false;
                DeleteButton.Checked = false;
                StartButton.Checked = false;
                ChangeMode();
            }
            DrawingWindow.Select();
        }
        #endregion

        #region Project menu events

        private void MainProgramMenu_MenuActivate(object sender, EventArgs e)
        {
            if (selectedLine != null)
                drawManager.RedrawWithSelectedLine(selectedLine);
        }

        private void NewProjectMenuItem_Click(object sender, EventArgs e)
        {
            if (proj == null)
            {
                proj = new NewProjectForm();
                proj.ShowDialog();
            }
            else if (proj.IsDisposed)
            {
                proj = new NewProjectForm();
                proj.ShowDialog();
            }
            else{
                proj.Clear();
                proj.ShowDialog();
                proj.BringToFront();
            }
        }

        private void saveProjectAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Title = "Pathfinder files";
            dlg.Filter = "Pathfinder files (*.paf)|*.paf";
            if (dlg.ShowDialog() == DialogResult.OK)
            {

                int i = dlg.FileName.LastIndexOf('\\');
                this.Text = "PathFinder - " + dlg.FileName.Substring(i+1);
                project.SaveAs(dlg.FileName);
            }
        }

        private void saveProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (project.ProjectPath != "")
            {
                project.Save();
            }
            else
            {
                saveProjectAsToolStripMenuItem_Click(sender, e);
            }
        }

        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Pathfinder files";
            dlg.Filter = "Pathfinder files (*.paf)|*.paf";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                project = Project.Load(dlg.FileName);
                if (project != null)
                {
                    DrawingWindow.AttachBackground(project.Size);
                    this.Text = "PathFinder - " +dlg.SafeFileName.Substring(0, dlg.SafeFileName.Length - 4);
                    if(project.HasImage && project.Image != null)
                        DrawingWindow.AttachBackground(project.Image);
                    drawManager = new DrawManager(DrawingWindow);
                    drawManager.Graph = project.Graph;
                    NewProjectInit(true);
                    MainForm_Resize(null, null);
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SelectedLineGroupBox_VisibleChanged(object sender, EventArgs e)
        {
            if (SelectedLineGroupBox.Visible == false)
            {
                massSelectIndex = 0;
                if(propertiesManager != null)
                    propertiesManager.massEdit = false;
                AllLinePropertiesSwitch(false);
                selectedLine = null;
                
                if (drawManager != null){
                    drawManager.Redraw();
                    drawManager.selectedLine = null;
                }
            }
            else
            {
                SlopePreviewPictureBox.Refresh();
            }
        }

        private void calculatePathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!propertiesManager.massEdit)
            {
                DrawLineButton.Checked = false;
                MoveButton.Checked = false;
                PropertiesButton.Checked = false;
                DeleteButton.Checked = false;
                StartButton.Checked = false;
                EndButton.Checked = false;
            }
            if (drawManager.IsReadyToCalculate())
            {
                drawManager.Graph.PrepareForWalkthrough();
                try
                {
                    if (drawManager.Graph.ShortestWay(null, null))
                    {
                        drawManager.Graph.PercentDone = 0;
                        int Odds = drawManager.Graph.GetOddPointsCount();
                        bool Alive = false;
                        if (thr != null)
                            if (!thr.IsAlive)
                                Alive = true;
                        if (thr == null || Alive)
                        {
                            thr = new Thread(new ThreadStart(drawManager.Graph.Walkthrough));
                            if (Odds > 16)
                            {
                                DialogResult msg = MessageBox.Show("Too many odd points, maximal number of odd points for fast calculation is 16,\n"
                                 + "calculations might take very long time. \n\nWould you like to continue?", "Notification", MessageBoxButtons.YesNo);
                                if (msg == DialogResult.Yes)
                                {
                                    thr.Start();
                                }
                                else return;
                            }
                            if (Odds > 12)
                            {
                                if (!thr.IsAlive)
                                    thr.Start();
                                WalkInfoForm fr = new WalkInfoForm(thr, drawManager.Graph);
                                if (fr.DialogResult == DialogResult.Cancel)
                                {
                                    return;
                                }
                            }
                            else if (!thr.IsAlive)
                                thr.Start();
                        }
                        else
                        {
                            MessageBox.Show("Calculation still in progress.", "Notification");
                            return;
                        }
                        while (thr.IsAlive)
                        {
                        }
                        drawManager.DrawCompletePath();
                        if (drawManager.ExportImage == null)
                        {
                            MessageBox.Show("The path does not contain any lines.", "Notification");
                        }
                        else if (MessageBox.Show("Would you like to save generated path?", "Notification", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            SaveFileDialog dlg = new SaveFileDialog();

                            dlg.Title = "JPEG image file";
                            dlg.Filter = "JPEG image file (*.jpg)|*.jpg";
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                try
                                {
                                    drawManager.ExportImage.Save(dlg.FileName);
                                }
                                catch (Exception exc) { }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("No path between start and end point.", "Notification");
                    }
                }
                catch (ThreadAbortException exc)
                {
                    MessageBox.Show("Calculation timeout.", "Notification");
                }
            }
            else
            {
                MessageBox.Show("Cannot calculate path without existing start and end points.", "Notification");
            }
        }

        private void fillAllLinesPropertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            massSelectIndex = 0;
            GraphLine ln = LoadFirstLine();
            if (ln != null)
            {
                AllLinePropertiesSwitch(true);
                selectedLine = ln;
                drawManager.selectedLine = selectedLine;
                propertiesManager.massEdit = true;
                drawManager.RedrawWithSelectedLine(selectedLine);
                propertiesManager.Load(selectedLine);
            }
            else
            {
                MessageBox.Show("No lines to edit.", "Notification");
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("The PathFinder is program for searching the shortest path from start point to end point through all required lines."
            + "\n\nThis program is final work in BI-BAP for the Czech technical university\nFaculty of information technologies.\n"
             + "This program was made in summer semester 2011/2012.\n\n"
             + "Author : Martin Zachov\n"
             + "Contact : martin.zachov@gmail.com", "About", MessageBoxButtons.OK);
        }

        private void manualToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Process.Start("manual.pdf");
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error while opening manual:\n" + exc.Message, "Notification");
            }
        }

        #endregion

        #region Project loading functions

        private void NewProjectInit(bool loadFromFile)
        {
            DrawLineButton.Enabled = true;
            MoveButton.Enabled = true;
            DeleteButton.Enabled = true;
            PropertiesButton.Enabled = true;
            DrawingWindow.Enabled = true;
            StartButton.Enabled = true;
            EndButton.Enabled = true;
            DrawingWindow.Visible = true;
            this.BackgroundImage = null;
            DrawLineButton.Visible = true;
            DeleteButton.Visible = true;
            MoveButton.Visible = true;
            PropertiesButton.Visible = true;
            StartButton.Visible = true;
            EndButton.Visible = true;

            DrawLineButton.Checked = false;
            DeleteButton.Checked = false;
            MoveButton.Checked = false;
            PropertiesButton.Checked = false;
            StartButton.Checked = false;
            EndButton.Checked = false;

            saveProjectAsToolStripMenuItem.Enabled = true;
            saveProjectToolStripMenuItem.Enabled = true;
            fillAllLinesPropertiesToolStripMenuItem.Enabled = true;
            calculatePathToolStripMenuItem.Enabled = true;
            this.DrawingWindow.drawablePicture.Click += new System.EventHandler(this.DrawingWindow_Click);
            this.DrawingWindow.drawablePicture.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawingWindow_MouseDown);
            this.DrawingWindow.drawablePicture.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DrawingWindow_MouseMove);
            this.DrawingWindow.drawablePicture.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DrawingWindow_MouseUp);
            if (drawManager != null && !loadFromFile)
            {
                drawManager.Clear();
            }
            DrawingWindow.Focus();
            ChangeMode();
            DrawingWindow.Select();
            DrawingWindow.drawablePicture.Select();
        }

        public void CreateNewProject(Image file)
        {
            DrawingWindow.AttachBackground(file);
            this.Text = "PathFinder - New project";
            drawManager = new DrawManager(DrawingWindow);
            project = new Project(drawManager.Graph, file);
            NewProjectInit(false);
            MainForm_Resize(null, null);
        }

        public void CreateNewProject(int w, int h)
        {
            DrawingWindow.AttachBackground(new Size(w, h));
            this.Text = "PathFinder - New project";
            drawManager = new DrawManager(DrawingWindow);
            project = new Project(drawManager.Graph, new Size(w, h));
            NewProjectInit(false);
            MainForm_Resize(null, null); 
        }


        
        #endregion

        #region Other functions

        private void ChangeMode()
        {
            SelectedLineGroupBox.Visible = false;
            selectedPoint = null;
            isselectedMoveitem = false;
            selectedLine = null;
            if (drawManager != null)
            {
                drawManager.Redraw();
                drawManager.selectedLine = null;
            }
        }

        public Point ValidPoint(Point pt)
        {
            Point ret = new Point(pt.X, pt.Y);
            
            if (pt.X > DrawingWindow.background.Size.Width - 10)
            {
                ret.X = DrawingWindow.background.Size.Width - 10; 
               
            }
            if (pt.X < 7)
            {
                ret.X = 7;
            }
            if (pt.Y > DrawingWindow.background.Size.Height - 8)
            {
                ret.Y = DrawingWindow.background.Size.Height - 8;
            }
            if (pt.Y < 7)
            {
                ret.Y = 7;
            }
            return ret;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            ManageDrawingWidowSize();
            SelectedLineGroupBox.Location = new Point(DrawingWindow.Location.X + DrawingWindow.Size.Width + 6, SelectedLineGroupBox.Location.Y);
            SelectedLineGroupBox.Size = new Size(this.Size.Width - SelectedLineGroupBox.Location.X - 28, SelectedLineGroupBox.Size.Height);
            previous = this.Size;
            MainForm_Activated(sender, e);
        }

        private void ManageDrawingWidowSize()
        {
            DrawingWindow.MaximumSize = new Size(Math.Min(this.Width - 240, DrawingWindow.drawablePicture.Width + 2),
               Math.Min(this.Height - 120, DrawingWindow.drawablePicture.Height + 2));
        }

        private void AllLinePropertiesSwitch(bool On)
        {
            if (On)
            {

                PropertiesButton.Checked = true;
                DrawLineButton.Enabled = false;
                MoveButton.Enabled = false;
                PropertiesButton.Enabled = false;
                DeleteButton.Enabled = false;
                StartButton.Enabled = false;
                EndButton.Enabled = false;

                SelectedLineGroupBox.Visible = true;
            }
            else
            {
                DrawLineButton.Enabled = true;
                MoveButton.Enabled = true;
                PropertiesButton.Enabled = true;
                DeleteButton.Enabled = true;
                StartButton.Enabled = true;
                EndButton.Enabled = true;

                SelectedLineGroupBox.Visible = false;
            }
        }
        #endregion

        #region All Lines Properties functions

        private GraphLine LoadFirstLine()
        {
            massSelectIndex++;
            if (drawManager.Graph.GraphLines.Count != 0)
            {
                selectedLine = drawManager.Graph.GraphLines[0];
                return drawManager.Graph.GraphLines[0];
            }
            return null;
        }

        public void LoadNextLine()
        {
            if (drawManager.Graph.GraphLines.Count == massSelectIndex)
            {
                massSelectIndex = 0;
                propertiesManager.massEdit = false;
                AllLinePropertiesSwitch(false);
            }
            else if (drawManager.Graph.GraphLines.Count > massSelectIndex)
            {
                GraphLine line = drawManager.Graph.GraphLines[massSelectIndex];
                massSelectIndex++;
                selectedLine = line;
                drawManager.RedrawWithSelectedLine(selectedLine);
                propertiesManager.Load(line);
            }

        }

        #endregion

        private void MainForm_Activated(object sender, EventArgs e)
        {
            DrawingWindow.Update();
            if (drawManager != null)
            {
                drawManager.Redraw();
                if (selectedLine != null)
                    drawManager.RedrawWithSelectedLine(selectedLine);
                propertiesManager.Load(selectedLine);
            }
        }

    }
}
